//
//  LZTEmoticonBottomView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/18.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

/*
 1.LZTEmoticonBottomView: UIView
 2.LZTEmoticonBottomView: UIStackView - 容器视图:给其设置背景颜色是没有意义的 - iOS9.0以后出来的
 */

import UIKit

//定义一个参数 tag的一个定量加数
private let tagNum = 2000

//底部按钮的枚举
enum LZTEmoticonBottomViewType: Int {
    case Default = 0
    case Emoji = 1
    case Lxh = 2
}

class LZTEmoticonBottomView: UIStackView {
    
    //闭包：告诉KeyboardView是哪一个button被点击了
    var closure:((type: LZTEmoticonBottomViewType)->())?
    
    //上一个选中的按钮
    var currentButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //提供方法：记录当前被点击的按钮
    func setupCurrentButton(tag: Int) {
        
        //得到被点击的button
        let button = viewWithTag(tag + tagNum) as! UIButton
        
        selectButton(button)
    }

    //底部按钮的点击事件
    func buttonClick(button: UIButton) {
        
        selectButton(button)
        
        //执行闭包
        closure?(type: LZTEmoticonBottomViewType(rawValue: button.tag - tagNum)!)
    }
    
    func selectButton(button: UIButton) {
        
        //如果当前选中按钮和上一次记录的按钮相同，就不走了
        if currentButton == button {
            return
        }
        
        //取消 上一个按钮的选中状态
        currentButton?.selected = false
        
        //当前选中的按钮的赋值
        button.selected = true
        currentButton = button
        
    }
    
    //设置视图
    func setupUI() {
        
        //填充方向
        axis = .Horizontal
        //填充方式 等比率
        distribution = .FillEqually
        
        addChildButtons("left", title: "默认", type: .Default)
        addChildButtons("mid", title: "Emoji", type: .Emoji)
        addChildButtons("right", title: "浪小花", type: .Lxh)
        
//      //UIView设定用
//      //三个按钮的添加
//        let defaultBtn = addChildButtons("left", title: "默认")
//        let emojiBtn = addChildButtons("mid", title: "Emoji")
//        let lxhBtn = addChildButtons("right", title: "浪小花")
//        //添加约束
//        defaultBtn.snp_makeConstraints { (make) in
//            make.top.leading.bottom.equalTo(self)
//            make.width.equalTo(emojiBtn)
//        }
//        emojiBtn.snp_makeConstraints { (make) in
//            make.top.bottom.equalTo(self)
//            make.leading.equalTo(defaultBtn.snp_trailing)
//            make.width.equalTo(lxhBtn)
//        }
//        lxhBtn.snp_makeConstraints { (make) in
//            make.top.bottom.trailing.equalTo(self)
//            make.leading.equalTo(emojiBtn.snp_trailing)
//        }
        
    }
    
    //添加底部按钮
    func addChildButtons(imgName: String, title: String, type: LZTEmoticonBottomViewType) {
        
        let button = UIButton()
        button.tag = type.rawValue + tagNum
        //添加点击事件
        button.addTarget(self, action: #selector(LZTEmoticonBottomView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //默认情况下 default是选中状态
        if type == .Default {
            button.selected = true
            currentButton = button
        }
        
        addArrangedSubview(button)
    }
}
